package net.bitacademy.java22.domain;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

// Profile을 DB에 저장하는 것을 담당하는 DAO
public class ProfileDAO extends SQLiteOpenHelper {
	SQLiteDatabase db;
	ContentValues row;
	//생성자 함수
	public ProfileDAO(Context context) {
		super(context, "pascal.db", null, 1);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS  hardware (tag TEXT PRIMARY KEY ASC , hwsetting INTEGER, swsetting INTEGER)");	
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS hardware");
		onCreate(db);
	}
	
	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
		db.execSQL("CREATE TABLE IF NOT EXISTS  hardware (tag TEXT PRIMARY KEY ASC , hwsetting INTEGER, swsetting INTEGER)");
	}
	
	
	public void insert2(ProfileVO pvo) {
		db = this.getWritableDatabase();
		row = new ContentValues();
		db.execSQL("INSERT INTO hardware (tag , hwsetting, swsetting ) VALUES(\""+pvo.getTag()+"\","+pvo.getHwSetting()+","+pvo.getSwSetting()+" )");
		this.close();
	}
	
	public String selectSwsetting(String tag) {
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT swsetting FROM hardware WHERE tag=\""+tag+"\"", null);
		String result = "";
		while (cursor.moveToNext()) {
			int id = cursor.getInt(0);
			result += id ;
		}
		return result;
	}
	
	
	
	
	
	
	
	
	
	
	public void deleteNormal(ProfileVO pvo) {
		db = this.getWritableDatabase();
		String [] del = {pvo.getTag()};
		db.delete("hardware", "tag=?", del);
		this.close();
	}
	
	// 작성자 : 이현우
	// 내용 : 이벤트 직전의 하드웨어 현재값 저장하기
	// 마지막 업데이트 날짜 : 2012-3-12
	public void setCurrentHW(ProfileVO pvo) {
		db = this.getWritableDatabase();
		db.execSQL("INSERT INTO hardware (tag,hwsetting) VALUES(\"#normal\","+pvo.getHwSetting()+")");
		this.close();
		db.close();
	}
	
	// 작성자 : 이현우
	// 내용 : 이벤트 직전의 저장된 하드웨어값 가져오기
	// 마지막 업데이트 날짜 : 2012-3-12	
	public ProfileVO getCurrentHW() {
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT * FROM hardware WHERE tag =\"#normal\"", null);
		ProfileVO pvo = new ProfileVO();
		while (cursor.moveToNext()) {
			pvo.setTag(cursor.getString(0));
			pvo.setHwSetting(cursor.getInt(1));
		}
		cursor.close();
		db.close();
		return pvo;
	}
	
	//작성자: 이현우
	//내용:	UI에서 submit 버튼을 누르면 최종적으로 호출되는 메소드
	//		ProfileHandler.create()에서 호출되며
	//		이곳에서 만든 값을 db에 저장한다.
	//최종수정날짜:	2012-02-23
	public void insert(ProfileVO pvo) {
		db = this.getWritableDatabase();
		row = new ContentValues();
		Integer a = pvo.getHwSetting();
		db.execSQL("INSERT INTO hardware (tag , hwsetting) VALUES( \""+pvo.getTag()+"\" ,  "+pvo.getHwSetting()+" )");
		this.close();
	}
	
	//작성자:	박상희, 정승현
	//내용:	delete 변수값으로 가능하게 함
	//최종수정날짜:	2012-02-24
	public void del(ProfileVO pvo) { //String deltag
		db = this.getWritableDatabase();
		String [] del = {pvo.getTag()};
		db.delete("hardware", "tag=?", del);
		this.close();
	}

	//select와 validation때 모두 사용
	public String select() {
		String result = "";
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT tag  FROM hardware", null);
		while (cursor.moveToNext()) {
			String tag = cursor.getString(0);
			if(tag != null) {
				result += tag+':';
			}
		}
		cursor.close();
		db.close();
		if (result.length() == 0) {
			return null;
		} else {
			return result;
		}
	}

	public String tagjoin(){
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT hardware.tag FROM calendarlist INNER JOIN hardware ON calendarlist.tag=hardware.tag ", null);
		String result = "";
		while (cursor.moveToNext()) {
			String tag = cursor.getString(0);
			result += tag;
		}
		cursor.close();
		db.close();
		if (result.length() == 0) {
			return "Empty Set";
		} else {
			return result;
		}
	}
	
	
	public ProfileVO detail(String tagname) {
		Log.d("detail", tagname);
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT * FROM hardware WHERE tag =\""+tagname+"\"", null);
		ProfileVO pvo = new ProfileVO();
		while (cursor.moveToNext()) {
			pvo.setTag(cursor.getString(0));
			pvo.setHwSetting(cursor.getInt(1));
			pvo.setSwSetting(cursor.getInt(2));
		}
		cursor.close();
		db.close();
		return pvo;
	}
	
	public void update(ProfileVO pvo) { 
		db = this.getWritableDatabase();
		pvo.getHwSetting();
		pvo.getTag();
		db.execSQL("UPDATE hardware SET hwsetting= "+pvo.getHwSetting()+"  WHERE tag= \""+pvo.getTag()+"\" ");
		detail(pvo.getTag());
		this.close();
	}
	
	
	public void update2(ProfileVO pvo) { 
		db = this.getWritableDatabase();
		pvo.getSwSetting();
		pvo.getTag();
		db.execSQL("UPDATE hardware SET swsetting= "+pvo.getSwSetting()+"  WHERE tag= \""+pvo.getTag()+"\" ");
		detail(pvo.getTag());
		this.close();
	}
	
	public String selectHwsetting(String tag) {
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT hwsetting FROM hardware WHERE tag=\""+tag+"\"", null);
		String result = "";
		while (cursor.moveToNext()) {
			int id = cursor.getInt(0);
			result += id ;
		}
		return result;
	}
}
